#测试文件分享页面，先从文件目录页获取其中一个文件的分享码，然后回到该页面进行分享
import time

from selenium.webdriver import ActionChains
from selenium.webdriver.common.by import By
from common.Utils import CloudDriver
from selenium.webdriver.support import expected_conditions as EC
class Share:
    url=""
    driver=""
    def __init__(self):
        self.url="http://vnollx.cloud/homepage"
        self.driver=CloudDriver.driver
        self.driver.get(self.url)

    def GainShareCode(self):
        #一开始进入文件目录页
        self.driver.find_element(By.CSS_SELECTOR, "#allfilelink").click()
        # 切换
        iframe = self.driver.find_element(By.TAG_NAME, "iframe")
        self.driver.switch_to.frame(iframe)
        #找到要分享的文件
        # file_elements=self.driver.find_element(By.XPATH,"/html/body/div[2]/table/tbody")
        # filenames = [elem.text for elem in file_elements]
        # 定位元素，右击选择
        elem = self.driver.find_element(By.CSS_SELECTOR, "#file-list > tr:nth-child(2) > td:nth-child(1)")
        # 定位
        EC.presence_of_element_located(elem)
        # 创建动作链对象
        actions = ActionChains(self.driver)
        # 右键点击元素
        actions.context_click(elem).perform()
        #点击进入该页面
        self.driver.find_element(By.CSS_SELECTOR,"#contextMenu > div:nth-child(4)").click()
        share_code=self.driver.find_element(By.CSS_SELECTOR,"#swal2-html-container > div:nth-child(1) > code").text
        self.driver.find_element(By.CSS_SELECTOR,"body > div.swal2-container.swal2-center.swal2-backdrop-show > div > button").click()
        self.driver.switch_to.default_content()
        #得到分享码调到该页面
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.sidebar > nav > ul > li:nth-child(4)").click()
        #转换到下文iframe中
        # 切换
        iframe = self.driver.find_element(By.TAG_NAME, "iframe")
        self.driver.switch_to.frame(iframe)
        #找到输入框
        self.driver.find_element(By.CSS_SELECTOR,"#shareCode").send_keys(share_code)
        self.driver.find_element(By.CSS_SELECTOR,"body > div.container > div.input-group > button").click()
        time.sleep(2)
        word=self.driver.find_element(By.CSS_SELECTOR,"#fileName").text
        print(word)
        assert word=="picture"

